﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;

namespace _sdnMap
{
    public partial class PropertyQuery : Form
    {
        public IMapControl2 pMapControl;
        public IMap pMap;
        public int iLayerIndex;
        public int iFieldIndex;
        public PropertyQuery(IMapControl2 pFMapControl)
        {
            InitializeComponent();
            pMapControl = pFMapControl;
            pMap = pFMapControl.Map;
        }


        private void PropertyQuery_Load(object sender, EventArgs e)
        {
            ILayer pLayer;
            for (int i = 0; i < pMap.LayerCount; i++)
            {
                pLayer = pMap.get_Layer(i);
                comboBoxLayer.Items.Add(pLayer.Name);
            }
        }

        //更改图层
        private void comboBoxLayer_TextChanged(object sender, EventArgs e)
        {
            iLayerIndex = comboBoxLayer.Items.IndexOf(comboBoxLayer.Text);
            comboBoxField.Items.Clear();
        }

        //更改字段
        private void comboBoxField_TextChanged(object sender, EventArgs e)
        {
            iFieldIndex = comboBoxField.Items.IndexOf(comboBoxField.Text);
        }

        //获取字段
        private void comboBoxField_DropDown(object sender, EventArgs e)
        {
            comboBoxField.Items.Clear();
            IFeatureLayer pFeatureLayer;
            pFeatureLayer = (IFeatureLayer)pMap.get_Layer(iLayerIndex);

            IFields pFields;
            pFields = pFeatureLayer.FeatureClass.Fields;


            for (int i = 0; i < pFields.FieldCount; i++)
            {
                string fieldName;
                fieldName = pFields.get_Field(i).Name;
                comboBoxField.Items.Add(fieldName);
            }
        }

        //显示该图层该字段的所有属性值
        private void buttonAllValue_Click(object sender, EventArgs e)
        {
            if (comboBoxField.Text == "")
            {
                MessageBox.Show("请选择字段名", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            listBoxValue.Items.Clear();
            IFeatureLayer pFeatureLayer;
            pFeatureLayer = (IFeatureLayer)pMap.get_Layer(iLayerIndex);

            IFeatureCursor pFeatureCursor;
            pFeatureCursor = pFeatureLayer.FeatureClass.Search(null, false);

            IFeature pFeature;
            pFeature = pFeatureCursor.NextFeature();
            while (pFeature != null)
            {
                string fldValue;
                if (comboBoxField.Text == "Shape")
                {
                    fldValue = Convert.ToString(pFeature.Shape.GeometryType);
                }
                else
                    fldValue = Convert.ToString(pFeature.get_Value(iFieldIndex));

                listBoxValue.Items.Add(fldValue);
                pFeature = pFeatureCursor.NextFeature();
            }
        }

        //从属性列表中双击选取属性值
        private void listBoxValue_DoubleClick(object sender, EventArgs e)
        {
            textBoxValue.Text = Convert.ToString(listBoxValue.SelectedItem);
        }

        private void buttonQuery_Click(object sender, EventArgs e)
        {
            IActiveView pActiveView;
            pActiveView = (IActiveView)pMap;
            pMap.ClearSelection();
            pActiveView.Refresh();

            IQueryFilter pQueryFilter = new QueryFilterClass();         

            IFeatureLayer pFeatureLayer;
            pFeatureLayer = (IFeatureLayer)pMap.get_Layer(iLayerIndex);

            IFields pFields;
            pFields = pFeatureLayer.FeatureClass.Fields;
            IField pField;
            pField=pFields.get_Field(iFieldIndex);

            switch(pField.Type)
            {
                case esriFieldType.esriFieldTypeString:
                    pQueryFilter.WhereClause=comboBoxField.Text + " = '" + textBoxValue.Text + "'";
                    break;
                case esriFieldType.esriFieldTypeDouble:
                case esriFieldType.esriFieldTypeInteger:
                case esriFieldType.esriFieldTypeSingle:
                case esriFieldType.esriFieldTypeSmallInteger:
                    pQueryFilter.WhereClause = comboBoxField.Text + " = " + textBoxValue.Text ;
                    break;
            }

            IFeatureCursor pFeatureCursor;
            pFeatureCursor = pFeatureLayer.FeatureClass.Search(pQueryFilter, false);

            IFeature pFeature;
            pFeature = pFeatureCursor.NextFeature();
            while (pFeature!=null)
            {
                pMap.SelectFeature(pFeatureLayer, pFeature);
                pFeature = pFeatureCursor.NextFeature();
            }


            pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
        
        }

        


    }
}
